package cn.tedu.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.db.sql.Wrapper;
import cn.tedu.web.entity.Administrator;
import cn.tedu.web.mapper.AdministratorMapper;
import cn.tedu.web.service.IAdministratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@RestController
@Api(value = "User", tags = { "User" }, description = "用户相关")
@RequestMapping("/api/administrator")
public class AdministratorController {
    @Autowired
    private IAdministratorService administratorService;
    @Autowired
    private AdministratorMapper administratorMapper;

    /**
     * mp分页查询
     * 
     * @param current   : 当前页面
     * @param size      : 页面大小
     * @param "records" : 查询到的数据列表
     */
    @ResponseBody
    @ApiOperation(value = "获取管理员列表")
    @RequestMapping("info")
    public ResponseEntity<Object> list(@RequestBody JSONObject param) {
        Long current = (long) 1;
        Long size = (long) 10;
        current = Long.parseLong(param.getString("current"));
        size = Long.parseLong(param.getString("size"));
        // System.out.println(param.getString("current"));
        return new ResponseEntity<>(administratorMapper.selectPage(new Page<>(current, size),
                new QueryWrapper<Administrator>().eq("deleted", 0)), HttpStatus.OK);
    }

    /**
     * 保存管理员账号
     */
    @PostMapping("save")
    public Integer save(@RequestBody Administrator administrator) {
        return administratorService.insert(administrator);
    }

    /**
     * 删除账号
     */
    @PostMapping("delete")
    public Integer delete(@RequestBody JSONObject param) {
        Long id = Long.parseLong(param.getString("id"));
        return administratorService.delete(id);
    }

    /**
     * 启用账号
     */
    @PostMapping("enable")
    public Integer enable(@RequestBody JSONObject param) {
        Long id = Long.parseLong(param.getString("id"));
        return administratorService.enable(id);
    }

    /**
     * 禁用账号
     */
    @PostMapping("disable")
    @ResponseBody
    public Integer disable(@RequestBody JSONObject param) {
        Long id = Long.parseLong(param.getString("id"));
        return administratorService.disable(id);
    }

    /**
     * 重置密码
     */
    @PostMapping("reset")
    public Integer reset(@RequestBody JSONObject param) {
        Long id = Long.parseLong(param.getString("id"));
        return administratorService.reset(id);
    }

    /**
     * 修改指定用户的密码
     * 
     * @param id       用户的ID
     * @param password 新密码
     */
    @PostMapping("edit")
    public Integer passwd(@RequestBody JSONObject param) {
        Long id = Long.parseLong(param.getString("id"));
        String password = param.getString("password");
        if (id == null || id < 1) {
            return 0;
        }
        if (StringUtils.isEmpty(password)) {
            // 当密码为空的时候，不允许修改
            return 0;
        }
        return administratorMapper.passwd(id, password);
    }
}
